/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#ifndef PLINTERRUPTMANAGER_H
#define PLINTERRUPTMANAGER_H

#include <stddef.h>
#include <machine/pc/processor/interrupthandler.h>
#include <machine/pc/processor/pagefaulthandler.h>
#include <lib/spinlock.h>

/**
 * The interrupt manager.
 */
class PLInterruptManager {
public:
	/**
	 * Constrcutor.
	 */
	PLInterruptManager();
	/**
	 * Register a interrupt handler.
	 *
	 * @param nInterruptNumber The interrupt to register for.
	 * @param pHandler The handler that handles the interrupt.
	 * @return True if the registration was successfull.
	 */
	bool registerInterruptHandler(size_t nInterruptNumber, InterruptHandler *pHandler);
	/**
	 * Register a interrupt debug handler.
	 *
	 * @param nInterruptNumber The interrupt to register for.
	 * @param pHandler The handler that handles the interrupt.
	 * @return True if the registration was successfull.
	 */
	bool registerInterruptHandlerDebugger(size_t nInterruptNumber, InterruptHandler *pHandler);
	/**
	 * Get the interrupt number for breakpoints.
	 *
	 * @return The interrupt number.
	 */
	inline size_t getBreakpointInterruptNumber();
	/**
	 * Get the interrupt number for debug.
	 *
	 * @return The interrupt number.
	 */
	inline size_t getDebugInterruptNumber();
	/**
	 * Set the interrupt table.
	 */
	void initialiseProcessor();
private:
	// Generate a interrupt output if we are in text mode and starting up.
	void interruptOutputPrint(InterruptState &interruptState);
	void interruptOutput(InterruptState &interruptState);
	void setNewError(int *errorNumber);
	// Called when an interrupt was triggered
	// param interruptState reference to the usermode/kernel state before the interrupt
	static void interrupt(InterruptState &interruptState);
	void setInterruptGate(size_t nInterruptNumber, uintptr_t interruptHandler, bool bUserspace);
	void setTaskGate(size_t nInterruptNumber, uint16_t tssSeg);
	PageFaultHandler m_pageFaultHandler;
	// Structure of a x86 protected-mode gate descriptor
	struct GateDescriptor {
		// Bits 0-15 of the offset
		uint16_t offset0;
		// The segment selector
		uint16_t selector;
		// Reserved, must be 0
		uint8_t res;
		// Flags
		uint8_t flags;
		// Bits 16-31 of the offset
		uint16_t offset1;
	} PACKED;
	// The interrupt descriptor table (IDT)
	GateDescriptor m_IDT[256];
	// The normal interrupt handlers
	InterruptHandler *m_pHandler[256];
	// The debugger interrupt handlers
	InterruptHandler *m_pDbgHandler[256];
	Spinlock m_Lock;
};

size_t PLInterruptManager::getBreakpointInterruptNumber() {
	return 3;
}

size_t PLInterruptManager::getDebugInterruptNumber() {
	return 1;
}

#endif
